﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Pavilion
{
    public class CColor
    {
        public CColor()
        {
            Color = new Color();
        }

        public CColor(Color color)
        {
            this.Color = color;
        }

        /// <summary>
        /// Converts hsb color to rgb color. It also sets Color variable to the converted Color.
        /// </summary>
        public Color ConvertHsbToRgb(float h, float s, float b, int a)
        {
            if (0 == s)
            {
                Color = new Color((byte)(b * 255), (byte)(b * 255), (byte)(b * 255), a);
                return Color;
            }

            float fMax, fMid, fMin;
            int iSextant, iMax, iMid, iMin;

            if (0.5 < b)
            {
                fMax = b - (b * s) + s;
                fMin = b + (b * s) - s;
            }
            else
            {
                fMax = b + (b * s);
                fMin = b - (b * s);
            }

            iSextant = (int)Math.Floor(h / 60f);
            if (300f <= h)
                h -= 360f;

            h /= 60f;
            h -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);

            if (0 == iSextant % 2)
                fMid = h * (fMax - fMin) + fMin;
            else
                fMid = fMin - h * (fMax - fMin);

            iMax = Convert.ToInt32(fMax * 255);
            iMid = Convert.ToInt32(fMid * 255);
            iMin = Convert.ToInt32(fMin * 255);

            if (iSextant == 1)
                Color = new Color((byte)iMid, (byte)iMax, (byte)iMin, (byte)a);
            else if (iSextant == 2)
                Color = new Color((byte)iMin, (byte)iMax, (byte)iMid, (byte)a);
            else if (iSextant == 3)
                Color = new Color((byte)iMin, (byte)iMid, (byte)iMax, (byte)a);
            else if (iSextant == 4)
                Color = new Color((byte)iMid, (byte)iMin, (byte)iMax, (byte)a);
            else if (iSextant == 5)
                Color = new Color((byte)iMax, (byte)iMin, (byte)iMid, (byte)a);
            else
                Color = new Color((byte)iMax, (byte)iMid, (byte)iMin, (byte)a);

            return Color;
        }

        /// <summary>
        /// Gets or sets the color.
        /// </summary>
        public Color Color { get; set; }
    }
}
